package com.wb3.meta.api.core.controller;

import com.wb3.meta.api.core.service.MyToysService;
import com.wb3.meta.api.core.vo.ReqMyToysVo;
import com.wb3.meta.common.annotation.NoPermission;
import com.wb3.meta.common.annotation.RequireAuth;
import com.wb3.meta.common.authentication.RequestHolder;
import com.wb3.meta.common.plugin.BaseResultController;
import com.wb3.meta.common.pojo.ResultT;
import com.wb3.meta.common.utils.UploadFileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@RestController
@Api(tags = "我的崽崽相关接口")
@RequestMapping("base/mytoys")
public class MyToysController extends BaseResultController<ReqMyToysVo> {

    @Autowired
    private MyToysService myToysService;
    @Autowired
    private UploadFileUtils uploadFileUtils;

    /**
     * 列表接口
     *
     * @param reqMyToysVo
     */
    @RequireAuth
    @Override
    @ApiOperation("列表接口")
    @GetMapping("list")
    public ResultT list(ReqMyToysVo reqMyToysVo) {
        reqMyToysVo.setUserId(RequestHolder.getUserId());
        return myToysService.getResModelListMapByConditions(reqMyToysVo);
    }


    /**
     * 明细接口
     *
     * @param reqMyToysVo
     */
    @RequireAuth
    @Override
    @ApiOperation("详情接口")
    @GetMapping("detail")
    public ResultT detail(ReqMyToysVo reqMyToysVo) {
        return myToysService.getResModelMapByConditions(reqMyToysVo);
    }

    /**
     * 新增or更新接口
     *
     * @param reqMyToysVo
     */
    @RequireAuth
    @Override
    @ApiOperation("更新接口")
    @PostMapping("update")
    public ResultT saveOrUpdate(@RequestBody ReqMyToysVo reqMyToysVo) {
        return myToysService.saveOrUpdate(reqMyToysVo);
    }

    /**
     * 删除接口
     *
     * @param reqMyToysVo
     */
    @NoPermission
    @Override
    @ApiOperation("删除接口")
    @PostMapping("delete")
    public ResultT delete(@RequestBody ReqMyToysVo reqMyToysVo) {
        return myToysService.delete(reqMyToysVo);
    }

    @RequireAuth
    @ApiOperation("绑定崽崽接口")
    @PostMapping("bind")
    public ResultT bindToy(@RequestBody ReqMyToysVo reqMyToysVo) {

        return myToysService.bindToy(reqMyToysVo);
    }
    @RequireAuth
    @ApiOperation("崽崽聊天接口")
    @PostMapping("chat")
    public ResultT chat(@RequestBody ReqMyToysVo reqMyToysVo) throws IOException {
        return myToysService.chat(reqMyToysVo);
    }

    @RequireAuth
    @ApiOperation("聊天-文字转语音")
    @PostMapping("chat/text2Audio")
    public ResultT text2Audio(@RequestBody ReqMyToysVo reqMyToysVo) throws IOException {
        return myToysService.text2Audio(reqMyToysVo);
    }

    @RequireAuth
    @ApiOperation("崽崽上传图片")
    @PostMapping("upload")
    public ResultT upload(@RequestBody MultipartFile file) {
        // 获取文件后缀名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 定义允许的图片格式
        Set<String> allowedFormats = new HashSet<>(Arrays.asList(".jpg", ".jpeg", ".png", ".gif"));

        // 检查文件格式
        if (!allowedFormats.contains(suffix.toLowerCase())) {
            // 如果文件格式不正确，返回错误信息
            return ResultT.fail("只能上传图片");
        }

        // 生成新的文件名
        String filename = System.currentTimeMillis() + "-" + RequestHolder.getUserId() + suffix;

        // 上传文件
        return uploadFileUtils.uploadFileToAliyun("user/toys", filename, file);
    }
    @RequireAuth
    @ApiOperation("分享聊天记录完成接口")
    @PostMapping("msg/share")
    public ResultT shareMsg(@RequestBody ReqMyToysVo reqMyToysVo) {
        return myToysService.shareMsg(reqMyToysVo);
    }
//    @ApiOperation("测试赠送")
//    @PostMapping("test/give")
//    public ResultT testGive() {
//        return myToysService.testGive();
//    }

    @RequireAuth
    @ApiOperation("重置聊天记录接口")
    @PostMapping("msg/reset")
    public ResultT resetChatMsg(@RequestBody ReqMyToysVo reqMyToysVo) {
        return myToysService.resetChatMsg(reqMyToysVo);
    }


    @ApiOperation("批量发送大姨-临时接口")
    @PostMapping("batch/send")
    public ResultT batchSend(@RequestBody ReqMyToysVo reqMyToysVo) {

        return myToysService.batchSend(reqMyToysVo);
    }

    @RequireAuth
    @ApiOperation("崽崽-生成故事接口")
    @PostMapping("story/create")
    public ResultT createStory(@RequestBody ReqMyToysVo reqMyToysVo) throws IOException {
        return myToysService.createStory(reqMyToysVo);
    }


    @RequireAuth
    @ApiOperation("恢复体力接口")
    @PostMapping("power/restore")
    public ResultT restorePower(@RequestBody ReqMyToysVo reqMyToysVo) throws IOException {
        return myToysService.restorePower(reqMyToysVo);
    }

}
